//
//  USInAppNotificationBanner.m
//  us
//
//  Created by karry on 2025/4/16.
//

#import "USInAppNotificationBanner.h"

@interface USInAppNotificationBanner ()
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIView *iconBgView;
@property (nonatomic, strong) CharacterView *characterView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, copy) void (^tapHandler)(void);
@property (nonatomic, assign) BOOL isVisible;
@end

@implementation USInAppNotificationBanner

+ (instancetype)sharedBanner {
    static USInAppNotificationBanner *banner;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        banner = [[USInAppNotificationBanner alloc] initWithFrame:CGRectZero];
    });
    return banner;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIWindow *keyWindow = UIWindow.keyWindow;
        if (![self isDescendantOfView:keyWindow]) {
            [keyWindow addSubview:self];
        }
        
        self.windowLevel = UIWindowLevelAlert + 1;
        [self makeKeyAndVisible];
        
        self.hidden = YES;
        self.frame = CGRectMake(22, -65, ScreenWidth -44, 65);
      
        _contentView = [[UIView alloc] init];
        _contentView.layer.cornerRadius = 14;
        _contentView.clipsToBounds = YES;
        _contentView.backgroundColor = UIColor.mi_1_color;
        [self addSubview:_contentView];
        
        _iconBgView = [[UIView alloc] init];
        [_contentView addSubview:_iconBgView];
    
        
        _characterView = [[CharacterView alloc]initWithFrame:CGRectMake(0, 0, 40, 40) character:USUser.user.relCharacter];
        [_iconBgView addSubview:_characterView];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textColor = UIColor.labelColor;
        [_contentView addSubview:_titleLabel];
        
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.font = [UIFont systemFontOfSize:14];
        _messageLabel.textColor = UIColor.secondaryLabelColor;
        [_contentView addSubview:_messageLabel];
    
        // Layout using Masonry
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [_iconBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_contentView).offset(12);
            make.centerY.equalTo(_contentView);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        
     
        
        [_characterView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_iconBgView);
            make.left.equalTo(_contentView).offset(12 + 40 + 8);
            make.right.equalTo(_contentView).offset(-12);
        }];
        
        [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_iconBgView);
            make.left.right.equalTo(_titleLabel);
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped)];
        [_contentView addGestureRecognizer:tap];
        
    }
    return self;
}

- (void)showWithAvatar:(BOOL)showAvatar
                 title:(NSString *)title
               message:(NSString *)message
            tapHandler:(void (^)(void))tapHandler {
    
    self.characterView.character = USUser.user.relCharacter;
    
    self.iconBgView.hidden = !showAvatar;
    
    if (showAvatar) {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_contentView).offset(12 + 40 + 8);
        }];
                
    }else{
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_contentView).offset(20);
        }];
        
    }
    
    self.titleLabel.text = title;
    self.messageLabel.text = message;
    self.tapHandler = tapHandler;
    
    
    if (!self.isVisible) {
        self.hidden = NO;
        self.isVisible = YES;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.jk_top = kStatusBarHeight;
        }];
    } else {
        // 只刷新内容，不重置动画
    }
    
    // 延迟自动隐藏（重置计时）
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hide) object:nil];
    [self performSelector:@selector(hide) withObject:nil afterDelay:3.0];
}

- (void)hide {
    if (!self.isVisible) return;
    self.isVisible = NO;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.jk_top = -self.jk_height;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)bannerTapped {
    if (self.tapHandler) {
        self.tapHandler();
    }
    [self hide];
}

@end

